package com.gcxy.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.conf.result.R;
import com.gcxy.dto.AddDivValDTO;
import com.gcxy.dto.DicValPageDTO;
import com.gcxy.entity.DataDicVal;
import com.gcxy.entity.User;
import com.gcxy.mapper.DataDicValMapper;
import com.gcxy.service.IDataDicValService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-25
 */
@Service
public class DataDicValServiceImpl extends ServiceImpl<DataDicValMapper, DataDicVal> implements IDataDicValService {

    @Autowired
    private IUserService userService;

    @Override
    public R addDivVal(AddDivValDTO addDivValDTO) {
        Object userIdObj = StpUtil.getLoginId();
        String userId = (String) userIdObj;
        LambdaQueryWrapper<DataDicVal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDicVal::getCategoryId, addDivValDTO.getCategoryId())
                .eq(DataDicVal::getValName, addDivValDTO.getValName());
        DataDicVal dataDicVal = getOne(queryWrapper);
        if (dataDicVal != null) {
            return R.Failed("该数据字典已存在");
        }
        DataDicVal dicVal=new DataDicVal();
        BeanUtils.copyProperties(addDivValDTO, dicVal);
        LocalDateTime now = LocalDateTime.now();
        dicVal.setValStatus(1);
        dicVal.setCreater(userService.getById(userId).getUserName());
        dicVal.setCreateTime(now);
        save(dicVal);
        return R.Success("新增成功！");
    }

    @Override
    public R getAllDivVal() {
        return R.Success(this.list());
    }

    @Override
    public R updateDivValStatus(Integer valId) {
        DataDicVal dataDicVal = getById(valId);
        if (dataDicVal == null) {
            return R.Failed("未找到对应的数据字典值");
        }
        if (dataDicVal.getValStatus() == 1) {
            dataDicVal.setValStatus(0);
        }else {
            dataDicVal.setValStatus(1);
        }
        LocalDateTime now = LocalDateTime.now();
        Object userIdObj = StpUtil.getLoginId();
        String userId = (String) userIdObj;
        dataDicVal.setUpdater(userService.getById(userId).getUserName());
        dataDicVal.setUpdateTime(now);
        updateById(dataDicVal);
        return R.Success("修改成功");
    }

    @Override
    public R getDivValByPage(DicValPageDTO dicValPageDTO) {
        LambdaQueryWrapper<DataDicVal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(dicValPageDTO.getValName()!=null,DataDicVal::getValName,dicValPageDTO.getValName())
                .eq(dicValPageDTO.getValStatus()!=null,DataDicVal::getValStatus,dicValPageDTO.getValStatus())
                .eq(dicValPageDTO.getCategoryId()!=null,DataDicVal::getCategoryId,dicValPageDTO.getCategoryId());
        Page<DataDicVal> dataDicValPage=new Page<>(dicValPageDTO.getPageNumber(),dicValPageDTO.getPageSize());
        dataDicValPage=page(dataDicValPage,queryWrapper);
        return R.Success(dataDicValPage);
    }
}
